package com.waterworld.watch.common.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import java.util.List;

/**
 * Created by nhuan on 2019/3/21.
 */

public class DbController {

    /**
     * Helper
     */
    private DaoMaster.DevOpenHelper mHelper;

    /**
     * DaoMaster
     */
    private DaoMaster mDaoMaster;

    /**
     * DaoSession
     */
    private DaoSession mDaoSession;

    private Context mContext;

    private MessageDao mMessageDao;
    private NotificationDao mNotificationDao;

    private static DbController mDbController;

    /**
     * 单例
     */
    public static DbController getInstance(Context context) {
        if (mDbController == null) {
            synchronized (DbController.class) {
                if (mDbController == null) {
                    mDbController = new DbController(context);
                }
            }
        }
        return mDbController;
    }

    public DbController(Context context) {
        mContext = context;
        mHelper = new DaoMaster.DevOpenHelper(context,"wtwd_watch.db",null);
        mDaoMaster =new DaoMaster(getWritableDatabase());
        mDaoSession = mDaoMaster.newSession();
        mMessageDao = mDaoSession.getMessageDao();
        mNotificationDao = mDaoSession.getNotificationDao();
    }

    /**
     * 获取可写数据库
     */
    private SQLiteDatabase getWritableDatabase(){
        if(mHelper == null){
            mHelper =new DaoMaster.DevOpenHelper(mContext,"wtwd_watch.db",null);
        }
        SQLiteDatabase db = mHelper.getWritableDatabase();
        return db;
    }

    /**
     * 会自动判定是插入还是替换
     */
    public void insertOrReplace(Message message){
        mMessageDao.insertOrReplace(message);
    }

    /**
     * 获取该群组的本地最新消息
     */
    public Message getLatestMsg(Integer groupId){
        return mMessageDao.queryBuilder().where(MessageDao.Properties.GroupId.eq(groupId)).orderCustom(MessageDao.Properties.MsgId,"DESC").limit(1).build().unique();
    }

    /**
     * 获取该群组的本地最早消息
     */
    public Message getOldestMsg(Integer groupId){
        return mMessageDao.queryBuilder().where(MessageDao.Properties.GroupId.eq(groupId)).orderCustom(MessageDao.Properties.MsgId,"ASC").limit(1).build().unique();
    }

    /**
     * 查询群组所有消息数据
     */
    public List<Message> searchAll(Integer groupId){
        return mMessageDao.queryBuilder().where(MessageDao.Properties.GroupId.eq(groupId)).orderAsc(MessageDao.Properties.MsgId).list();
    }

    /**
     * 查询群组是否有未读消息
     */
    public boolean hasUnreadMsg(Integer groupId) {
        return !(mMessageDao.queryBuilder().where(MessageDao.Properties.GroupId.eq(groupId)).where(MessageDao.Properties.State.eq(0)).build().list().size() == 0);
    }

    /**
     * 查询某ID之后的信息
     */
    public List<Message> searchAfterMsgId(Integer groupId,Integer msgId) {
        return mMessageDao.queryBuilder().where(MessageDao.Properties.GroupId.eq(groupId)).where(MessageDao.Properties.MsgId.gt(msgId)).orderCustom(MessageDao.Properties.MsgId,"ASC").build().list();
    }

    /**
     * 查询某ID之前的的信息
     */
    public List<Message> searchBeforeMsgId(Integer groupId,Integer msgId) {
        return mMessageDao.queryBuilder().where(MessageDao.Properties.GroupId.eq(groupId)).where(MessageDao.Properties.MsgId.lt(msgId)).orderCustom(MessageDao.Properties.MsgId,"ASC").build().list();
    }

    /**
     * 是否需要插入消息通知
     */
    public void isNeedtoInsert(Notification notification) {
        if (mNotificationDao.queryBuilder().where(NotificationDao.Properties.MsgId.eq(notification.getMsgId())).build().list().size() == 0) {
            mNotificationDao.insertOrReplace(notification);
        }
    }

    /**
     * 更新通知消息
     */
    public void updateNotification(Notification notification) {
        if (mNotificationDao.queryBuilder().where(NotificationDao.Properties.MsgId.eq(notification.getMsgId())).build().list().size() > 0) {
            notification.setId(mNotificationDao.queryBuilder().where(NotificationDao.Properties.MsgId.eq(notification.getMsgId())).build().list().get(0).getId());
            mNotificationDao.insertOrReplace(notification);
        }
    }
    /**
     * 查询通知
     */
    public List<Notification> getNotification() {
        return mNotificationDao.queryBuilder().where(NotificationDao.Properties.State.notEq(2)).orderDesc(NotificationDao.Properties.MsgId).build().list();
    }

    /**
     * 查询groupId通知
     */
    public List<Notification> getNotification(Integer groupId) {
        return mNotificationDao.queryBuilder().where(NotificationDao.Properties.GroupId.eq(groupId)).where(NotificationDao.Properties.State.notEq(2)).orderDesc(NotificationDao.Properties.MsgId).build().list();
    }
}
